package com.wangjiang.mario.admin.quartz.controller;

import com.wangjiang.mario.admin.common.annotation.LogTrack;
import com.wangjiang.mario.admin.common.rest.ResultGenerator;
import com.wangjiang.mario.admin.common.rest.entity.Result;
import com.wangjiang.mario.admin.quartz.domain.SysJob;
import com.wangjiang.mario.admin.quartz.exception.TaskException;
import com.wangjiang.mario.admin.quartz.service.ISysJobService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 调度任务信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/monitor/job")
public class SysJobController
{

    @Autowired
    private ISysJobService jobService;

    @RequiresPermissions("monitor:job:view")
    @GetMapping("/id/{jobId}")
    public Result job(@PathVariable Long jobId)
    {
        return ResultGenerator.genSuccessResult(jobService.selectJobById(jobId));
    }

    @RequiresPermissions("monitor:job:list")
    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value = "任务列表")
    @LogTrack(title = "任务列表")
    public Result list(SysJob job)
    {
        List<SysJob> list = jobService.selectJobList(job);
        return ResultGenerator.genSuccessResult(list);
    }

//    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("monitor:job:export")
//    @PostMapping("/export")
//    @ResponseBody
//    public Result export(SysJob job)
//    {
//        List<SysJob> list = jobService.selectJobList(job);
//        ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
//        return util.exportExcel(list, "定时任务");
//    }

//    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @RequiresPermissions("monitor:job:remove")
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(@RequestParam String ids) throws SchedulerException
    {
        jobService.deleteJobByIds(ids);
        return ResultGenerator.genSuccessResult(true);
    }

//    @RequiresPermissions("monitor:job:detail")
//    @GetMapping("/detail/{jobId}")
//    public String detail(@PathVariable("jobId") Long jobId, ModelMap mmap)
//    {
//        mmap.put("name", "job");
//        mmap.put("job", jobService.selectJobById(jobId));
//        return prefix + "/detail";
//    }

    /**
     * 任务调度状态修改
     */
//    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("monitor:job:changeStatus")
    @PostMapping("/{jobId}/changeStatus")
    @ResponseBody
    public Result changeStatus(@PathVariable Long jobId) throws SchedulerException
    {
        SysJob newJob = jobService.selectJobById(jobId);
        String newStatus = Objects.equals("1", newJob.getStatus()) ? "0":"1"; // 修改状态
        newJob.setStatus(newStatus);
        return ResultGenerator.genSuccessResult(jobService.changeStatus(newJob));
    }

    /**
     * 任务调度立即执行一次
     */
//    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("monitor:job:changeStatus")
    @PostMapping("/run/{jobId}")
    @ResponseBody
    public Result run(@PathVariable Long jobId) throws SchedulerException
    {
        SysJob job = jobService.selectJobById(jobId);
        jobService.run(job);
        return ResultGenerator.genSuccessResult(true);
    }

//    /**
//     * 新增调度
//     */
//    @GetMapping("/add")
//    public String add()
//    {
//        return prefix + "/add";
//    }

    /**
     * 新增保存调度
     */
//    @Log(title = "定时任务", businessType = BusinessType.INSERT)
//    @RequiresPermissions("monitor:job:add")
    @PostMapping("/add")
    @ResponseBody
    public Result addSave(@Validated @RequestBody SysJob job) throws SchedulerException, TaskException
    {
        return ResultGenerator.genSuccessResult(jobService.insertJob(job));
    }

//    /**
//     * 修改调度
//     */
//    @GetMapping("/edit/{jobId}")
//    public String edit(@PathVariable("jobId") Long jobId, ModelMap mmap)
//    {
//        mmap.put("job", jobService.selectJobById(jobId));
//        return prefix + "/edit";
//    }

    /**
     * 修改保存调度
     */
//    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("monitor:job:edit")
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(@Validated @RequestBody SysJob job) throws SchedulerException, TaskException
    {
        return ResultGenerator.genSuccessResult(jobService.updateJob(job));
    }

    /**
     * 校验cron表达式是否有效
     */
    @PostMapping("/checkCronExpressionIsValid")
    @ResponseBody
    public boolean checkCronExpressionIsValid(@RequestBody SysJob job)
    {
        return jobService.checkCronExpressionIsValid(job.getCronExpression());
    }
}
